var prefix = jsctx + "/orderInfo";

var hlhtOrderInfo = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 120;

    function load() {

        var userTenantCode = $("#userTenantCode").val();
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listHlhtOrderInfo",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    // pageSize: 10,
                    pageList: [10, 20, 30],
                    // pageList: [10],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    rightFixedColumns: true,
                    showColumns: true,
                    // fixedNumber: 5,
                    // columns: columns,
                    // showPaginationSwitch:true,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            status: $("#status").val().trim(),
                            userSide: $("#userSide").val(),
                            equipmentSide: $("#equipmentSide").val(),
                            startTime: $("#startTime").val().trim(),
                            endTime: $("#endTime").val().trim(),
                            orderNum: $("#orderNum").val().trim(),
                            // epName: $("#epName").val().trim(),
                            gunNo: $("#gunNo").val().trim(),
                            totalMoney: $("#totalMoney").val().trim(),
                            carNum: $("#carNum").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {
                            field: 'status', title: '状态', width: '60', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >未充电</span>'
                                } else if (value == '1') {
                                    return '<span >充电中</span>'
                                } else if (value == '2') {
                                    return '<span >完成</span>'
                                } else if (value == '3') {
                                    return '<span >充电结算中</span>'
                                }
                            }
                        },
                        {field: 'stationName', title: '桩群', width: '120', align: 'left'},
                        {
                            title: '互联运营商', field: 'hlhtOperatorName', width: '110', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.orderInfoHlhtType == '0') {
                                    if ((row.hlhtTenantCode != null && row.hlhtTenantCode != '') && (row.hlhtOperatorId == null || row.hlhtOperatorId == '')) {
                                        return row.hlhtTenantName
                                    } else {
                                        return row.hlhtOperatorName
                                    }
                                } else if (row.orderInfoHlhtType == '1') {
                                    return row.hlhtTenantName
                                } else if (row.orderInfoHlhtType == '3') {
                                    return row.hlhtTenantName
                                } else {
                                    return row.hlhtTenantName
                                }
                            }
                        },
                        {
                            field: 'hlhtOrderType', title: '订单类型', width: '85', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.orderInfoHlhtType == '0') {
                                    if (row.hlhtOrderType == '0') {
                                        return '<span >自有订单</span>'
                                    } else if (row.hlhtOrderType == '1') {
                                        return '<span >反向订单</span>'
                                    } else if (row.hlhtOrderType == '2') {
                                        return '<span >正向订单</span>'
                                    }
                                } else if (row.orderInfoHlhtType == '1') {
                                    if (row.hlhtOrderType == '1') {
                                        return '<span >反向订单</span>'
                                    } else if (row.hlhtOrderType == '2') {
                                        return '<span >正向订单</span>'
                                    }
                                } else if (row.orderInfoHlhtType == '2') {
                                    return '<span >正向订单</span>'
                                } else if (row.orderInfoHlhtType == '3') {
                                    return '<span >正向内部外联订单</span>'
                                }
                            }
                        },
                        {
                            field: 'hlhtOrderType', title: '互联类型', width: '85', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.orderInfoHlhtType == '0') {
                                    if (value == '0') {
                                        return '<span ></span>'
                                    } else if ((row.hlhtTenantCode != null && row.hlhtTenantCode != '') && (row.hlhtOperatorId == null || row.hlhtOperatorId == '')) {
                                        return '<span >内部互联</span>'
                                    } else {
                                        return '<span >外部互联</span>'
                                    }
                                } else if (row.orderInfoHlhtType == '1') {
                                    return '<span >内部互联</span>'
                                } else if (row.orderInfoHlhtType == '3' ) {
                                    return '<span >外部互联</span>'
                                } else {
                                    return '<span >内部互联</span>'
                                }
                            }
                        },
                        {field: 'tenantName', title: '设备方', width: '110', align: 'left'},
                        {field: 'hlhtOperatorName', title: '用户方', width: '110', align: 'right'},
                        {field: 'gunNo', title: '枪编码', width: '85', align: 'left'},
                        {field: 'orderNum', title: '订单号', width: '130', align: 'left'},
                        {field: 'outOrderNum', title: '外部订单号', width: '130', align: 'left'},
                        {field: 'nickname', title: '会员名', width: '80', align: 'left'},
                        {field: 'mobile', title: '手机号', width: '110', align: 'left'},
                        {field: 'carNum', title: '车牌', width: '90', align: 'left'},
                        {
                            field: 'accountType', title: '充电方式', width: '85', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >个人充电</span>'
                                } else if (value == '1') {
                                    return '<span >企业充电</span>'
                                } else if (value == '2') {
                                    return '<span >共享充电</span>'
                                }
                            }
                        },
                        {field: 'startTime', title: '开始时间', width: '90', align: 'left'},
                        {field: 'endTime', title: '结束时间', width: '90', align: 'left'},
                        {field: 'lastTime', title: '充电时长', width: '80', align: 'left'},
                        {field: 'totalPower', title: '外部互联电量<br>(度)', width: '65', align: 'right'},
                        {field: 'totalElecMoney', title: '外部互联电费<br>(元)', width: '65', align: 'right'},
                        {field: 'totalSeviceMoney', title: '外部互联服务费<br>(元)', width: '70', align: 'right'},
                        {field: 'discountAmount', title: '外部互联优惠<br>(元)', width: '65', align: 'right'},
                        {
                            field: 'totalMoney', title:
                                '外部互联总价<br>(元)', width:
                                '65', align:
                                'right'
                        }
                        ,
                        {
                            field: 'payAmount', title:
                                '外部互联实付<br>(元)', width:
                                '65', align:
                                'right'
                        }
                        ,
                        {field: 'intranetTotalPower', title: '内部互联电量<br>(元)', width: '65', align: 'right'},
                        {field: 'intranetTotalElecMoney', title: '内部互联电费<br>(元)', width: '65', align: 'right'},
                        {field: 'intranetTotalSeviceMoney', title: '内部互联服务费<br>(元)', width: '65', align: 'right'},
                        {field: 'intranetDiscount', title: '内部互联优惠<br>(元)', width: '65', align: 'right'},
                        {field: 'intranetTotalMoney', title: '内部互联总价<br>(元)', width: '65', align: 'right'},
                        {field: 'intranetPayAmount', title: '内部互联实付<br>(元)', width: '65', align: 'right'},
                        // {field: 'discountServiceAmount', title: '折后服务费', width: '100', align: 'center'},
                        // {field: 'discountName', title: '优惠类型', width: '100', align: 'center'},
                        /**
                         * 停止方式
                         * 0: 用户手动停止充电；
                         * 1: 客户归属地运营商平台停止充电；
                         * 2: BMS 停止充电；
                         * 3: 充电机网络异常；
                         * 4: 连接器断开
                         * 5: 电价异常
                         * 6: 余额不足
                         * 7: 电流过低停止
                         * 8: 充满平台停止
                         * 9: 充满停止
                         * 10: 电池温度过高停止
                         * 11: 电流过高
                         * 12: 电压过高
                         * 13: 订单已结算
                         * 14: 刷卡停止
                         * 15: 超时停止
                         */

                        {
                            field: 'pileStopReasonVo', title:
                                '停止方式', width:
                                '100', align:
                                'left'
                        }
                        ,
                        {
                            field: 'stopReason', title:
                                '停止原因', width:
                                '100', align:
                                'left', class:
                                'operateBefore-td',
                            formatter:

                                function (value, row, index) {
                                    if (value == '0') {
                                        return '<span >手动停止充电</span>'
                                    } else if (value == '1') {
                                        return '<span >客户归属地运营商平台停止充电</span>'
                                    } else if (value == '2') {
                                        return '<span >BMS停止</span>'
                                    } else if (value == '3') {
                                        return '<span >充电机网络异常</span>'
                                    } else if (value == '4') {
                                        return '<span >连接器断开</span>'
                                    } else if (value == '5') {
                                        return '<span >电价异常</span>'
                                    } else if (value == '6') {
                                        return '<span >余额不足</span>'
                                    } else if (value == ' 7') {
                                        return '<span >电流过低停止</span>'
                                    } else if (value == '8') {
                                        return '<span >充满平台停止</span>'
                                    } else if (value == '9') {
                                        return '<span >充满停止</span>'
                                    } else if (value == '10') {
                                        return '<span >电池温度过高停止</span>'
                                    } else if (value == '11') {
                                        return '<span >电流过高</span>'
                                    } else if (value == '12') {
                                        return '<span >电压过高</span>'
                                    } else if (value == '13') {
                                        return '<span >订单已结算</span>'
                                    } else if (value == '14') {
                                        return '<span >刷卡停止</span>'
                                    } else if (value == '15') {
                                        return '<span >超时停止</span>'
                                    } else if (value == '16') {
                                        return '<span >温度阈值</span>'
                                    } else if (value == null) {
                                        return '<span ></span>'
                                    }
                                }
                        }
                        ,
                        {
                            field: 'beginSoc', title:
                                '开始SOC', width:
                                '65', align:
                                'right'
                        }
                        ,
                        {
                            field: 'soc', title:
                                '结束SOC', width:
                                '65', align:
                                'right'
                        }
                        ,
                        // {field: 'servicePriceAdjustType', title: '调整服务费单价系数', width: '100', align: 'right'},
                        // {field: 'servicePriceAdjustValue', title: '调后服务费单价', width: '100', align: 'right'},
                        // {field: 'serviceAdjustAmount', title: '调后服务费', width: '100', align: 'right'},
                        // {field: 'serviceAdjustDiscAmount', title: '调后优惠', width: '100', align: 'right'},
                        // {field: 'adjustTotalMoney', title: '调后总价', width: '100', align: 'right'},
                        // {field: 'adjustPayAmount', title: '调后实付', width: '100', align: 'right'},
                        // {field: 'adjustServiceDiffAmount', title: '服务费价差', width: '100', align: 'right'},
                        //
                        // {field: 'highPower', title: '峰时电量', width: '100', align: 'right'},
                        // {field: 'flatPower', title: '平时电量', width: '100', align: 'right'},
                        // {field: 'lowPower', title: '谷时电量', width: '100', align: 'right'},
                        // {field: 'highBaseAmount', title: '峰时电费', width: '100', align: 'right'},
                        // {field: 'flatBaseAmount', title: '平时电费', width: '100', align: 'right'},
                        // {field: 'lowBaseAmount', title: '谷时电费', width: '100', align: 'right'},
                        // {field: 'highServiceAmount', title: '峰时服务费', width: '100', align: 'right'},
                        // {field: 'flatServiceAmount', title: '平时服务费', width: '100', align: 'right'},
                        // {field: 'lowServiceAmount', title: '谷时服务费', width: '100', align: 'right'},
                        {
                            title: '操作',
                            field:
                                'operate',
                            width:
                                '150',
                            align:
                                'center',
                            class:
                                'operate-td',
                            formatter:

                                function (value, row, index) {
                                    // if (row.status!='2'){
                                        var a = '<a class="operateIcon stopIcon fl' + has_edit + '" href="#" title="结束订单" onclick="hlhtOrderInfo.stopOrderInfo(\''
                                            + row.orderId
                                            + '\')"></a> ';

                                        var b = '<a class="operateIcon printerIcon' + has_edit + '" href="#" title="打印小票" onclick="hlhtOrderInfo.printReceipt(\''
                                            + row.orderId
                                            + '\')"></a> ';
                                        var c = '<a class="operateIcon detailIcon ' + has_edit + '" href="#" title="订单详情" onclick="hlhtOrderInfo.orderContent(\''
                                            + row.orderId
                                            + '\')"></a> ';

                                        return a + b + c ;
                                    // }else {
                                    //     var a = '<a class="operateIcon stopIcon fl' + has_edit + '" href="#" title="结束订单" onclick="orderInfo.stopOrderInfo(\''
                                    //         + row.orderId
                                    //         + '\')"></a> ';
                                    //
                                    //     var b = '<a class="operateIcon printerIcon' + has_edit + '" href="#" title="打印小票" onclick="orderInfo.printReceipt(\''
                                    //         + row.orderId
                                    //         + '\')"></a> ';
                                    //     var c = '<a class="operateIcon detailIcon ' + has_edit + '" href="#" title="订单详情" onclick="orderInfo.orderContent(\''
                                    //         + row.orderId
                                    //         + '\')"></a> ';
                                    //     var d = '<a class="operateIcon exceptionIcon fr' + has_edit + '" href="#" title="异常修复" onclick="orderInfo.addExceptionOrderInfo(\''
                                    //         + row.orderId
                                    //         + '\')"></a> ';
                                    //     return a+b+c+d;
                                    // }
                                }
                        }
                    ],
                })
        ;
    }


    function exportOrderInfo() {
        var hlhtOperatorId;
        if ($("#hlhtOperatorId").length > 0) {
            hlhtOperatorId = $("#hlhtOperatorId").val();
        } else {
            hlhtOperatorId = '';
        }
        $.fileDownload(prefix + "/exportHlhtOrderInfo", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                // nickname: $("#nickname").val().trim(),
                // tenantCompanyId: $("#tenantCompanyId").val().trim(),
                hlhtOperatorId: hlhtOperatorId,
                // hlhtType: $("#hlhtType").val().trim(),
                status: $("#status").val(),
                startTime: $("#startTime").val(),
                // epName: $("#epName").val().trim(),
                mobile: $("#mobile").val(),
                endTime: $("#endTime").val(),
                gunNo: $("#gunNo").val(),
                orderNum: $("#orderNum").val(),
                totalMoney: $("#totalMoney").val(),
                // hlhtOrderType: $("#hlhtOrderType").val().trim(),
                carNum: $("#carNum").val(),
                // accountType:$("#accountType").val(),
                count: $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })

    }

    function stopOrderInfo(orderId) {
        layer.confirm('确定要结束选中的订单(结束后用户订单将直接被结算)？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/stopOrderInfo",
                type: "post",
                data: {
                    'orderId': orderId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("结束成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })

    }

    function printReceipt(orderId) {
//  $("#recipient-name").val(sort)
        //  $("#ulId").val(ulId)
        $('#exampleModal').modal('show')

        $("#savePrintReceipt").unbind("click").bind("click", function () {

            var formData = new FormData();
            formData.append("carNum", $("#saveCarNum").val());
            formData.append("orderId", orderId);
            $.ajax({
                url: prefix + "/savePrintReceipt",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        //  $('#exampleModal').modal('hide')
                    }

                }
            })
        })

        $("#resetPrintReceipt").unbind("click").bind("click", function () {

            var formData = new FormData();
            formData.append("carNum", $("#carNum").val());
            formData.append("orderId", orderId);
            $.ajax({
                url: prefix + "/resetPrintReceipt",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        //  $('#exampleModal').modal('hide')
                    }

                }
            })
        })


        // $("#giftMember").click(function () {
        //
        //
        // })
    }

    function reLoad() {
        var startTime = $("#startTime").val();
        if (startTime == '' || startTime == null) {
            parent.layer.alert("请选择开始时间");
            return;
        }
        $(".fixed-table-header-columns").css("display", "none");
        $(".fixed-table-body-columns").css("display", "none");
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    //订单详情
    function orderContent(orderId) {
        location.href = prefix + "/orderContent/" + orderId;
    }

    //异常订单数据修复登记
    function addExceptionOrderInfo(orderId) {
        $('#exampleModal1').modal('show')

        $("#submitData").unbind("click").bind("click", function () {

            var formData = new FormData();
            formData.append("orderId",orderId);
            formData.append("startTime", $("#startTime1").val());
            formData.append("endTime", $("#endTime1").val());
            // formData.append("useTime", $("#useTime").val());
            formData.append("totalPower", $("#totalPower").val());
            formData.append("totalElecMoney", $("#elecMoney").val());
            formData.append("totalServiceMoney", $("#serviceMoney").val());
            formData.append("discountAmount", $("#discountAmount").val());
            formData.append("totalMoney", $("#totalMoney").val());
            formData.append("payAmount", $("#payAmount").val());
            formData.append("beginSoc", $("#beginSoc").val());
            formData.append("soc", $("#soc").val());
            formData.append("solution", $("#solution").val());
            $.ajax({
                url:  "/exceptionOrder/saveExceptionOrderInfo",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal1').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm1").reset();
                        })
                        $('#exampleModal1').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal1').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm1").reset();
                        })
                        //  $('#exampleModal').modal('hide')
                    }

                }
            })
        })
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        orderContent: orderContent,
        exportOrderInfo: exportOrderInfo,
        stopOrderInfo: stopOrderInfo,
        printReceipt: printReceipt,
        addExceptionOrderInfo: addExceptionOrderInfo
    };


})();
